// Copyright 2022 The MediaPipe Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.mediapipe.tasks.text.textclassifier;

import com.google.auto.value.AutoValue;
import com.google.mediapipe.tasks.components.container.proto.CategoryProto;
import com.google.mediapipe.tasks.components.container.proto.ClassificationsProto;
import com.google.mediapipe.tasks.components.containers.Category;
import com.google.mediapipe.tasks.components.containers.ClassificationEntry;
import com.google.mediapipe.tasks.components.containers.Classifications;
import com.google.mediapipe.tasks.core.TaskResult;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/** Represents the classification results generated by {@link TextClassifier}. */
@AutoValue
public abstract class TextClassificationResult implements TaskResult {

  /**
   * Creates an {@link TextClassificationResult} instance from a {@link
   * ClassificationsProto.ClassificationResult} protobuf message.
   *
   * @param classificationResult a {@link ClassificationsProto.ClassificationResult} protobuf
   *     message.
   * @param timestampMs a timestamp for this result.
   */
  // TODO: consolidate output formats across platforms.
  static TextClassificationResult create(
      ClassificationsProto.ClassificationResult classificationResult, long timestampMs) {
    List<Classifications> classifications = new ArrayList<>();
    for (ClassificationsProto.Classifications classificationsProto :
        classificationResult.getClassificationsList()) {
      classifications.add(classificationsFromProto(classificationsProto));
    }
    return new AutoValue_TextClassificationResult(
        timestampMs, Collections.unmodifiableList(classifications));
  }

  @Override
  public abstract long timestampMs();

  /** Contains one set of results per classifier head. */
  @SuppressWarnings("AutoValueImmutableFields")
  public abstract List<Classifications> classifications();

  /**
   * Converts a {@link CategoryProto.Category} protobuf message to a {@link Category} object.
   *
   * @param category the {@link CategoryProto.Category} protobuf message to convert.
   */
  static Category categoryFromProto(CategoryProto.Category category) {
    return Category.create(
        category.getScore(),
        category.getIndex(),
        category.getCategoryName(),
        category.getDisplayName());
  }

  /**
   * Converts a {@link ClassificationsProto.ClassificationEntry} protobuf message to a {@link
   * ClassificationEntry} object.
   *
   * @param entry the {@link ClassificationsProto.ClassificationEntry} protobuf message to convert.
   */
  static ClassificationEntry classificationEntryFromProto(
      ClassificationsProto.ClassificationEntry entry) {
    List<Category> categories = new ArrayList<>();
    for (CategoryProto.Category category : entry.getCategoriesList()) {
      categories.add(categoryFromProto(category));
    }
    return ClassificationEntry.create(categories, entry.getTimestampMs());
  }

  /**
   * Converts a {@link ClassificationsProto.Classifications} protobuf message to a {@link
   * Classifications} object.
   *
   * @param classifications the {@link ClassificationsProto.Classifications} protobuf message to
   *     convert.
   */
  static Classifications classificationsFromProto(
      ClassificationsProto.Classifications classifications) {
    List<ClassificationEntry> entries = new ArrayList<>();
    for (ClassificationsProto.ClassificationEntry entry : classifications.getEntriesList()) {
      entries.add(classificationEntryFromProto(entry));
    }
    return Classifications.create(
        entries, classifications.getHeadIndex(), classifications.getHeadName());
  }
}
